﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;
using System.Threading;
using work_log.userservices;

namespace work_log {
    public partial class frmPassword : DevExpress.XtraEditors.XtraForm {
        /// <summary>
        /// UI线程的同步上下文
        /// </summary>
        private SynchronizationContext PassContext = null;
        /// <summary>
        /// 是否校验通过
        /// </summary>
        bool fCheck1 = false, fCheck2 = false;

        userservices.ResultInfoOfUsers result = null;
        userservices.Users entity = null;
        public frmPassword() {
            InitializeComponent();
            PassContext = SynchronizationContext.Current;
        }

        private void frmPassword_Load(object sender, EventArgs e) {
            this.txtName.Text = UserInfo.uCode;
            txtOldPwd.Select();
        }

        private void frmPassword_Shown(object sender, EventArgs e) {
            txtPwd.LostFocus += txtControl_LostFocus;
            txtConPwd.LostFocus += txtControl_LostFocus;
        }

        private void txtControl_LostFocus(object sender, EventArgs e) {
            string reg = "";
            TextBox tb = ((TextBox)sender);
            if (tb.Focused) return;
            switch (tb.Tag.ToString()) {
                case "txtPwd":
                    reg = @"^[a-zA-z0-9]\w{5,17}$";
                    if (tb.Text.Trim() == "") {
                        lblMSG.Text = "新密码不能为空！";
                        lblMSG.ForeColor = Color.Red;
                        lblMSG.Font = new Font("楷体", 14);
                        fCheck1 = false;
                    } else if (!Regex.IsMatch(tb.Text, reg, RegexOptions.Compiled)) {
                        lblMSG.Text = "密码长度为6~18位，并且只能由大小写字母、数字以及下划线组成！";
                        lblMSG.ForeColor = Color.Red;
                        lblMSG.Font = new Font("楷体", 9);
                        fCheck1 = false;
                    } else if (MD5Class.MD5String(tb.Text) == UserInfo.uPassword) {
                        lblMSG.Text = "新密码不能与旧密码相同！";
                        lblMSG.ForeColor = Color.Red;
                        lblMSG.Font = new Font("楷体", 14);
                        fCheck1 = false;
                    } else {
                        lblMSG.Text = "";
                        fCheck1 = true;
                    }
                    break;
                case "txtConPwd":
                    if (tb.Text.Trim() == "") {
                        lblMSG.Text = "确认密码不能为空！";
                        lblMSG.Font = new Font("楷体", 14);
                        lblMSG.ForeColor = Color.Red;
                        fCheck2 = false;
                    } else if (tb.Text.Trim() != txtPwd.Text) {
                        lblMSG.Text = "两次密码不一致！";
                        lblMSG.ForeColor = Color.Red;
                        lblMSG.Font = new Font("楷体", 14);
                        fCheck2 = false;
                    } else {
                        lblMSG.Text = "";
                        fCheck2 = true;
                    }
                    break;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e) {
            if (txtOldPwd.Text.Trim() == string.Empty) {
                lblMSG.Text = "旧密码不能为空！";
                lblMSG.ForeColor = Color.Red;
                lblMSG.Font = new Font("楷体", 14);
                return;
            }
            if (!fCheck1) return;
            if (!fCheck2) return;

            this.progressPanel1.Visible = true;
            this.btnConfirm.Enabled = false;

            entity = new Users();
            entity.uCode = UserInfo.uCode;
            entity.uPassword = MD5Class.MD5String(txtPwd.Text);
            entity.UpdateDate = DateTime.Now;

            Thread PassThread = new Thread(new ThreadStart(this.EditPass));
            PassThread.Start();
        }

        #region Custom Opertion
        private void EditPass() {
            try {
                result = new UserServicesSoapClient().Update(entity, UserInfo.oldPass);
                PassContext.Post(PassSafeOpertion, null);
            } catch (Exception ex) {
                PassContext.Post(PassSafeOpertion, "12");
                XtraMessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region ThreadSafe Opertion
        private void PassSafeOpertion(object sender) {
            if (sender == null) {
                if (result.Result) {
                    XtraMessageBox.Show(result.MSG);
                    this.progressPanel1.Visible = false;
                    this.btnConfirm.Enabled = true;
                    this.Close();
                } else {
                    XtraMessageBox.Show(result.MSG);
                    this.progressPanel1.Visible = false;
                    this.btnConfirm.Enabled = true;
                }
            } else {
                this.progressPanel1.Visible = false;
                this.btnConfirm.Enabled = true;
            }
        }
        #endregion

        private void frmPassword_FormClosing(object sender, FormClosingEventArgs e) {
            try {
                this.Close();
            } catch (Exception) {
            }
        }
    }
}